﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;
using TarreOA.TarreValidationCode;

public partial class DiscussArea_DiscussIndex : System.Web.UI.Page
{
    private PagedDataSource pds = null;
    private int da_id;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString.Get("da_id") != null)
            {
                da_id = int.Parse(Request.QueryString.Get("da_id"));
                this.NewPagedDataSource();
                this.DataList_DataBind();
                ViewState["pg_in"] = pds.CurrentPageIndex;
                ViewState["da_id"] = da_id;
            }
        }
    }

    private void NewPagedDataSource()
    {
        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = 10;
        this.DataList_DataTable();
    }

    protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
    {
        string code = Code.Codes;
        if (ViewState["da_id"] != null)
        {
            da_id = (int)ViewState["da_id"];
        }
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int)ViewState["pg_in"];
        }
        if (Request.Form.Get(this.TxtValidationCode.ClientID).Trim().Equals(code))
        {
            if (!"".Equals(Request.Form.Get(this.TxtTitle.ClientID)) && !"".Equals(Request.Form.Get(this.TxtContent.ClientID)))
            {
                DiscussThemetbService dtService = new DiscussThemetbService();
                string title = Request.Form.Get(this.TxtTitle.ClientID);
                string content = Request.Form.Get(this.TxtContent.ClientID);
                User curLoginUser = (User)Session["user"];
                if (dtService.SaveDiscussThemeInfo(title, content, DateTime.Now, curLoginUser.UserId, da_id) == 1)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('主题发表成功')", true);
                    this.DataList_DataTable();
                    this.DataList_DataBind();
                }
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('验证码错误！');", true);
        }
        this.TxtTitle.Text = "";
        this.TxtContent.value = "";
        this.TxtValidationCode.Text = "";
        this.DataList_DataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["da_id"] = da_id;
    }

    private void DataList_DataTable()
    {
        DiscussThemetbService dtService = new DiscussThemetbService();
        DiscussThemeCommentstbService dtcService = new DiscussThemeCommentstbService();
        DataTable dt = new DataTable();
        dt.Columns.Add("DiscussThemeId", typeof(string));
        dt.Columns.Add("DiscussThemeTitle", typeof(string));
        dt.Columns.Add("DiscussThemeAuthor", typeof(string));
        dt.Columns.Add("DiscussThemeCreateTime", typeof(string));
        dt.Columns.Add("DiscussThemeHot", typeof(string));
        dt.Columns.Add("DiscussThemeAppCount", typeof(string));
        dt.Columns.Add("DiscussThemeLastAppTime", typeof(string));
        dt.Columns.Add("DiscussThemeLastAppUser", typeof(string));
        List<DiscussThemetb> list = dtService.findALLByDaId(da_id);
        if (list != null && list.Count > 0)
        {
            foreach (DiscussThemetb discussTheme in list)
            {
                DataRow dr = dt.NewRow();
                dr["DiscussThemeId"] = discussTheme.DiscussThemeId.ToString();
                dr["DiscussThemeTitle"] = discussTheme.DiscussThemeTitle;
                dr["DiscussThemeAuthor"] = discussTheme.DiscussThemeAuthor.UserName;
                dr["DiscussThemeCreateTime"] = discussTheme.DiscussThemeCreateTime.ToString();
                dr["DiscussThemeHot"] = discussTheme.DiscussThemeHot.ToString();
                dr["DiscussThemeAppCount"] = discussTheme.DiscussAppCount.ToString();
                DiscussThemeCommentstb dtcTb = dtcService.FindTheOneByLastReadTime(discussTheme.DiscussThemeId);
                if (dtcTb != null)
                {
                    dr["DiscussThemeLastAppTime"] = dtcTb.DtCommentsCreateTime.ToString();
                    dr["DiscussThemeLastAppUser"] = dtcTb.DtCommentsAuthor.UserName;
                }
                dt.Rows.Add(dr);
            }
            pds.DataSource = dt.DefaultView;
        }
        else
        {
            pds.DataSource = null;
        }
    }

    private void DataList_DataBind()
    {
        if (pds.DataSource == null)
        {
            this.DataList1.DataSource = null;
        }
        else
        {
            this.DataList1.DataSource = pds;
        }
        this.DataList1.DataBind();
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        if (ViewState["da_id"] != null)
        {
            da_id = (int)ViewState["da_id"];
        }
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int)ViewState["pg_in"];
        }
        pds.CurrentPageIndex--;
        this.DataList_DataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["da_id"] = da_id;
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        if (ViewState["da_id"] != null)
        {
            da_id = (int)ViewState["da_id"];
        }
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int)ViewState["pg_in"];
        }
        pds.CurrentPageIndex++;
        this.DataList_DataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        ViewState["da_id"] = da_id;
    }
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string dtid = ((HtmlInputHidden)e.Item.FindControl("HidDtId")).Value;
            ((HyperLink)e.Item.FindControl("HylTitle")).NavigateUrl = "ThemeAppear.aspx?dt_id=" + dtid;
        }

        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (pds.IsFirstPage)
            {
                ((ImageButton)e.Item.FindControl("ImageButton3")).Enabled = false;
            }
            if (pds.IsLastPage)
            {
                ((ImageButton)e.Item.FindControl("ImageButton2")).Enabled = false;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Index.aspx");
    }
}
